package com.fourpigeon.domain;

import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fourpigeon.utils.LocalDateTimeSerializerUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static com.fourpigeon.utils.SystemConstants.userInfoMatchSet;

/**
 * 含用户部分信息的User类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
    /**
     * 用户id
     */
    @TableId(value = "id")
    private Long id;
    /**
     * 微信用户id
     */
    private String openId;
    /**
     * 用户名称
     */
    private String name;
    /**
     * 用户性别：0：男，1：女，2：保密
     */
    private Short sex = 2;
    /**
     * 密码
     */
    private String password = "";
    /**
     * 用户头像
     */
    private String icon = "icon";
    /**
     * 手机号
     */
    private String phone = "";
    /**
     * 自我介绍
     */
    private String introduce = "这个人很懒，没有自我介绍";
    /**
     * 粉丝数量
     */
    private Integer fans = 0;
    /**
     * 关注数量
     */
    private Integer followee = 0;
    /**
     * 生日
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime birthday;
    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime updateTime;

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public User(Long id,String openId,String name){
        this.id = id;
        this.openId = openId;
        this.name = name;
        this.birthday = LocalDateTime.of(2022,1,1,0,0);
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    /**
     * 更新个人信息的方法(不处理密码、粉丝、关注者)
     * @param user 前端数据
     * @return 结果
     */
    public User matchAndUpdate(UserInfo user) {
        //结果列表，用于返回不同的元素的key
        List<Object> resultList = new ArrayList<>();
        Map<String, Object> oriMap = BeanUtil.beanToMap(this);
        Map<String, Object> newMap = BeanUtil.beanToMap(user);
        for (String key : newMap.keySet()) {
            //如果为密码、关注、粉丝，跳过
            if(userInfoMatchSet.contains(key)) continue;
            //不相同，更新map中的数据
            if (!newMap.get(key).equals(oriMap.get(key))){
                oriMap.remove(key);
                oriMap.put(key,newMap.get(key));
                resultList.add(key);
            }
        }
        if (resultList.isEmpty()) {
            return null;
        }
        //mapToBean
        User temp = BeanUtil.mapToBean(oriMap, User.class, false);
        return temp;
    }
}
